home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
pctj8401.arc
/
WCDEBUG.C
< prev
Wrap
Text File
|
1983-09-28
|
2KB
|
72 lines
/* Figure 2 - wc with debugging printf statements */
/* wc.c - count words */
#include <stdio.h>
#define WORD 0
#define NOT_WORD 1
FILE *fd ;
main(argc,argv)
int argc ;
char *argv[] ;
{
int c ;
long wc ;
if( argc < 2 )
{ printf("\n no name");
exit(0) ;
}
fd = fopen(argv[1],"r"); /* open the file */
if( fd == 0 )
{ printf("\n can't open");
exit(0) ;
}
printf(" fd= %d \n",fd) ;
wc = 0 ;
while( skip(NOT_WORD) != EOF ) /* skip to beginning of next word */
{
printf("\n after skip(NOT_WORD) \n");
wc++ ;
if( skip(WORD) == EOF ) /* skip to end of the word */
break ;
printf("\n after skip(WORD) \n");
} ;
fclose(fd) ;
printf("\n %ld words",wc);
}
int check(c) /* classify a char as part of word or not */
{
printf(" check-%x",c);
c = c & 0x7f ;
if( (c == ' ')
|| (c == '\c')
|| (c == '\n')
|| (c == ',')
|| (c == '.')
|| (c == '(')
|| (c == ')') )
return( NOT_WORD ) ;
else return( WORD ) ;
}
int skip(skip_type) /* skip chars of skip_type in file fd */
int skip_type ;
{
int c ;
printf("\n skip called - skip_type= %d \n",skip_type);
c = getc(fd) ;
while( (c != EOF) && (check(c) == skip_type ) )
{ printf(" loop-%x",c) ;
c = getc(fd) ;
} ;
return( c ) ;
}